package com.jeegit.ecms.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jeegit.common.annotation.Log;
import com.jeegit.common.enums.BusinessType;
import com.jeegit.ecms.domain.CmsCollectionCategory;
import com.jeegit.ecms.service.ICmsCollectionCategoryService;
import com.jeegit.ecms.utils.ECMSUtils;
import com.jeegit.common.core.controller.BaseController;
import com.jeegit.common.core.domain.AjaxResult;
import com.jeegit.common.utils.poi.ExcelUtil;
import com.jeegit.common.utils.StringUtils;
import com.jeegit.common.core.domain.Ztree;

/**
 * 栏目分类Controller
 * 
 * @author jeegit
 * @date 2022-06-27
 */
@Controller
@RequestMapping("/ecms/collectioncategory")
public class CmsCollectionCategoryController extends BaseController
{
    private String prefix = "ecms/collectioncategory";

    @Autowired
    private ICmsCollectionCategoryService cmsCollectionCategoryService;

    @RequiresPermissions("ecms:collectioncategory:view")
    @GetMapping()
    public String collectioncategory(CmsCollectionCategory cmsCollectionCategory,ModelMap mmap)
    { 
    	mmap.put("cmsCollectionCategory", cmsCollectionCategory);
    	mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
        return prefix + "/collectioncategory";
    }

    /**
     * 查询栏目分类树列表
     */
    @RequiresPermissions("ecms:collectioncategory:list")
    @PostMapping("/list")
    @ResponseBody
    public List<CmsCollectionCategory> list(CmsCollectionCategory cmsCollectionCategory)
    {
        List<CmsCollectionCategory> list = cmsCollectionCategoryService.selectCmsCollectionCategoryList(cmsCollectionCategory);
        return list;
    }

    /**
     * 导出栏目分类列表
     */
    @RequiresPermissions("ecms:collectioncategory:export")
    @Log(title = "栏目分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CmsCollectionCategory cmsCollectionCategory)
    {
        List<CmsCollectionCategory> list = cmsCollectionCategoryService.selectCmsCollectionCategoryList(cmsCollectionCategory);
        ExcelUtil<CmsCollectionCategory> util = new ExcelUtil<CmsCollectionCategory>(CmsCollectionCategory.class);
        return util.exportExcel(list, "栏目分类数据");
    }

    /**
     * 新增栏目分类
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsCollectionCategory", cmsCollectionCategoryService.selectCmsCollectionCategoryById(id));
        }
        mmap.put("cmsCollectionCategory", new CmsCollectionCategory());
        mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
        return prefix + "/add";
    }

    /**
     * 新增保存栏目分类
     */
    @RequiresPermissions("ecms:collectioncategory:add")
    @Log(title = "栏目分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CmsCollectionCategory cmsCollectionCategory)
    {
        return toAjax(cmsCollectionCategoryService.insertCmsCollectionCategory(cmsCollectionCategory));
    }

    /**
     * 修改栏目分类
     */
    @RequiresPermissions("ecms:collectioncategory:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CmsCollectionCategory cmsCollectionCategory = cmsCollectionCategoryService.selectCmsCollectionCategoryById(id);
        //初始化树结构
        ECMSUtils.initECMSTreeData(cmsCollectionCategory);
        mmap.put("cmsCollectionCategory", cmsCollectionCategory);
        mmap.put("cmsSiteList", ECMSUtils.getCmsSiteList());
        return prefix + "/edit";
    }

    /**
     * 修改保存栏目分类
     */
    @RequiresPermissions("ecms:collectioncategory:edit")
    @Log(title = "栏目分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CmsCollectionCategory cmsCollectionCategory)
    {
        return toAjax(cmsCollectionCategoryService.updateCmsCollectionCategory(cmsCollectionCategory));
    }

    /**
     * 删除
     */
    @RequiresPermissions("ecms:collectioncategory:remove")
    @Log(title = "栏目分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(cmsCollectionCategoryService.deleteCmsCollectionCategoryById(id));
    }

    /**
     * 选择栏目分类树
     */
    @GetMapping(value = { "/selectCollectioncategoryTree/{id}", "/selectCollectioncategoryTree/" })
    public String selectCollectioncategoryTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("cmsCollectionCategory", cmsCollectionCategoryService.selectCmsCollectionCategoryById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载栏目分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = cmsCollectionCategoryService.selectCmsCollectionCategoryTree();
        return ztrees;
    }
}
